﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.DAO;
using E_Shop.Common;
using ESHOP.UTIL;
using ESHOP.Domain;

namespace E_Shop
{
    public partial class TrademallProductDetail : Authentication
    {

        private readonly DanhMucDAO _danhmuc = new DanhMucDAO();
        private SanPhamDAO _sanphamDao = new SanPhamDAO();
        private GioHangDAO _giohangDao = new GioHangDAO();
        private SANPHAM _sanpham = new SANPHAM();
        private String IDSP
        {
            get
            {
                string id = Request["id"];
                return id;
            }
        }
        public SANPHAM obj
        {
            get
            {
                var sp = _sanphamDao.Get(IDSP);
                return sp;
            }
            set
            {
                value = _sanphamDao.Get(IDSP);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request["id"];
            _sanpham = _sanphamDao.Get(id);
            obj = _sanpham;
            Image1.ImageUrl = "../Images/Product/" + _sanpham.HINHANH + "";
            lbProductName.Text = _sanpham.TENSANPHAM;
            lbProductDec.Text = _sanpham.CHITIET;
            lbProductPrice.Text = "US $" + _sanpham.GIA.ToString();
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (IDSP != null)
            {
                SANPHAM sp = new SANPHAM();
                sp = _sanphamDao.Get(IDSP);
                GIOHANG gioHang = new GIOHANG();
                gioHang.MACT = Acount.MACT;
                gioHang.MASP = IDSP;
                gioHang.SOLUONG = 1;
                gioHang.GIA = sp.GIA;
                gioHang.ISSHOW = true;
                _giohangDao.Insert(gioHang);
                Session[Constants.SESSION_GIOHANG] = gioHang;
                Response.Redirect("ShoppingCheckOut.aspx");
            }
        }
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            SANPHAM sp = new SANPHAM();
            sp = _sanphamDao.Get(IDSP);
            GIOHANG gioHang = new GIOHANG();
            var giohangs = _giohangDao.GetListBuyCompanyId(Acount.MACT);
            int flag = 1;
            for (int i = 0; i < giohangs.Count; i++)
            {
                if (giohangs[i].MASP == IDSP)
                {
                    _giohangDao.UpdateNumber(giohangs[i].ID, 1);
                    flag = 0;
                    break;
                }
            }
            if (flag == 1)
            {
                
                gioHang.MACT = Acount.MACT;
                gioHang.MASP = IDSP;
                gioHang.SOLUONG = 1;
                gioHang.GIA = sp.GIA;
                gioHang.ISSHOW = true;
                _giohangDao.Insert(gioHang);
                
            }            
            Session[Constants.SESSION_GIOHANG] = gioHang;
            Response.Redirect("ShoppingDefault.aspx", true);
        }
    }
}